﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QY.Util;
using System.Web;
using System.IO;
using Loong.Model;

namespace QYWebSiteBN
{
    public partial class Form_AddUrl : Form
    {



        private List<UrlModelInfo> urlRuleList;

        public Form_AddUrl()
        {           
            InitializeComponent();
        }

        public Form_AddUrl(UrlModelInfo index) : this()
        {
       
           
           updateUI(index);
           
           
        }


        public URLModelRule UrlModel
        {
            get 
            { 
                  Form_EditTack formef = (Form_EditTack)this.Owner;

                  return formef.UrlModel;
            }
            set
            {
                Form_EditTack formef = (Form_EditTack)this.Owner;
                formef.UrlModel = value;
 
            }
        }


        private void updateUI(UrlModelInfo info)
        {
          //  UrlModelInfo info = UrlModel.Get(index);
           tabControl1.SelectedIndex = Convert.ToInt32(info.Type);
           int begin=info.Name.IndexOf('<');
           int end=info.Name.IndexOf('>');

           
           switch (info.Type)
           {
               case 0:
                   {
                       txtUrl.Text = info.Name;
                       break;
                   }
               case 1:
                   {
                      
                       
                       string value = info.Name.Substring(begin + 1, end - begin - 1);
                       string[] vs = value.Split(',');
                       txtVarBegin.Text = vs[0];
                       txtVarEnd.Text = vs[1];
                       txtInterval.Text = vs[2];
                       txtApproxiUrl.Text= info.Name.Replace(info.Name.Substring(begin,end-begin+1),"(*)");
                       cbOrder.Checked = Convert.ToBoolean(vs[3]);
                       cbZero.Checked = Convert.ToBoolean(vs[4]);
                       return;
 
                   }           
           }      
        
           
        }

        private void btnSingleAdd_Click(object sender, EventArgs e)
        {
           
                if (urlRuleList == null)//如果列表未存在
                {
                    urlRuleList = new List<UrlModelInfo>();//创建
                }
                else
                {
                    for (var i = 0; i < urlRuleList.Count; i++)
                    {

                        if (urlRuleList[i].Name.ToLower() == txtUrl.Text.ToLower())
                        {

                            MessageBox.Show("数据不能重复,请重新设置!");
                            return;
                        }
                    }
                }

                for (var i = 0; i < UrlModel.Count; i++)
                {
                    if (UrlModel.Get(i).Name == txtUrl.Text)
                    {
                        MessageBox.Show("数据不能重复,请重新设置!");
                        return;
                    }
                }

                if (!string.IsNullOrEmpty(txtUrl.Text))
                {
                    lbUrl.Items.Add(txtUrl.Text);
                    urlRuleList.Add(new UrlModelInfo(0, txtUrl.Text));
                }           
        }



        //批量网址预览
        private void btnPreview_Click(object sender, EventArgs e)
        {
            check();
                int begin=Convert.ToInt32(txtVarBegin.Text);
                int end=Convert.ToInt32(txtVarEnd.Text);
                int interval = Convert.ToInt32(txtInterval.Text);
                lbPreview.Items.Clear();

                if (cbOrder.Checked)
                {
                    for (int i=0;i<=3;i++)
                    {
                        lbPreview.Items.Add(txtApproxiUrl.Text.Replace("(*)", end.ToString()));
                        end -=interval;
                    }
                    lbPreview.Items.Add("...");
                    lbPreview.Items.Add(txtApproxiUrl.Text.Replace("(*)", begin.ToString()));

                    
                }
                else
                {
                    for (int i = 0; i <= 3; i++)
                    {
                        lbPreview.Items.Add(txtApproxiUrl.Text.Replace("(*)", begin.ToString()));
                        begin +=interval;
                    }
                    lbPreview.Items.Add("...");
                    lbPreview.Items.Add(txtApproxiUrl.Text.Replace("(*)", end.ToString()));

                }

            
        }

        private void tabPage2_Click(object sender, EventArgs e)
        {
           

        }



        private void btnMultUrl_Click(object sender, EventArgs e)
        {
            check();
            string s = string.Format("<{0},{1},{2},{3},{4}>", txtVarBegin.Text, txtVarEnd.Text, txtInterval.Text, cbOrder.Checked.ToString(), cbZero.Checked.ToString());
            s=txtApproxiUrl.Text.Replace("(*)",s);

            if (UrlModel.Count > 0)
            {
                for (var i = 0; i < UrlModel.Count; i++)
                {
                    if (UrlModel.Get(i).Name.ToLower() == s.ToLower())
                    {

                        MessageBox.Show("数据不能重复,请重新设置!");
                        return;
                    }
                }
            }

            if (urlRuleList == null)
            {
                urlRuleList = new List<UrlModelInfo>();
            }
            else
            {
                for (var i = 0; i < urlRuleList.Count; i++)
                {
                    
                    if (urlRuleList[i].Name.ToLower() ==s.ToLower())
                    {
                       
                        MessageBox.Show("数据不能重复,请重新设置!");
                        return;
                    }
                }
            }

    
            lbUrl.Items.Add(s);          
            urlRuleList.Add(new UrlModelInfo(1,s));
                      
            
        }


        private void check()
        {
            if (string.IsNullOrEmpty(txtApproxiUrl.Text))
            {
                MessageBox.Show("地址形式不能为空");
                txtApproxiUrl.Focus();
                return;
            }

            if (txtApproxiUrl.Text.IndexOf("(*)") < 0)
            {
                MessageBox.Show("地址形式必须通配符(*)!");
                txtApproxiUrl.Focus();
                return;
            }


            if (string.IsNullOrEmpty(txtVarBegin.Text))
            {
                MessageBox.Show("范围起始值不能为空");
                txtVarBegin.Focus();
                return;
            }


            if (string.IsNullOrEmpty(txtVarEnd.Text))
            {
                MessageBox.Show("范围结束值不能为空");
                txtVarEnd.Focus();
                return;
            }



            if (!Regular.IsURL(txtApproxiUrl.Text))
            {
                MessageBox.Show("地址形式要求有网址格式(如:http://www.qy.com.cn/)");
                txtApproxiUrl.Focus();
                return;
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (lbUrl.SelectedIndices.Count<1)
            {
                MessageBox.Show("没有选中项!");
                return;
            }

            for(int i=0;i<lbUrl.SelectedIndices.Count;i++)
            {
                lbUrl.Items.RemoveAt(i);
                if (urlRuleList != null)
                {
                    if (urlRuleList[i] != null)
                    {
                        urlRuleList.RemoveAt(i);
                    }
                }
            }           
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            lbUrl.Items.Clear();
            if (urlRuleList != null)
            {
                urlRuleList.Clear();
            }

        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            
            if (urlRuleList != null)
            {
                 for(int i=0;i<urlRuleList.Count;i++)
                 {
                     UrlModel.Add(urlRuleList[i]);
                 }
             }       

           

             Form_EditTack formef=(Form_EditTack)this.Owner;
             string dir=  formef.StoreDir();
             string file = string.Format("{0}\\{1}.task.url",dir,formef.TaskName());

             formef.UpdateTask();
             this.Close();
         
        }

        
    }
}
